<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.1.1">
<meta name="baidu-site-verification" content="code-hkfwkpUecU" />
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="baidu-site-verification" content="true">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Monda:300,300italic,400,400italic,700,700italic|Roboto+Slab:300,300italic,400,400italic,700,700italic|PT+Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.14.0/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"github.com","root":"/","scheme":"Gemini","version":"8.0.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"disqus","storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>

  <meta name="description" content="列表 列表是python中的一个对象  数值只能保存一个单一的数据，列表中可以保存多个有序的数据  列表的创建：通过[] 创建  12list &#x3D; []  # 创建了一个空列表print(list, type(list))  # [] &lt;class &amp;#x27;list&amp;#x27;&gt;     一个列表中可以存储多个元素，也可以在创建列表时，指定列表中的元素  123list &#x3D; [5]">
<meta property="og:type" content="article">
<meta property="og:title" content="数据结构">
<meta property="og:url" content="https://github.com/shanlu123/2020/09/27/python/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/index.html">
<meta property="og:site_name" content="shanlu的博客">
<meta property="og:description" content="列表 列表是python中的一个对象  数值只能保存一个单一的数据，列表中可以保存多个有序的数据  列表的创建：通过[] 创建  12list &#x3D; []  # 创建了一个空列表print(list, type(list))  # [] &lt;class &amp;#x27;list&amp;#x27;&gt;     一个列表中可以存储多个元素，也可以在创建列表时，指定列表中的元素  123list &#x3D; [5]">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-09-26T16:00:00.000Z">
<meta property="article:modified_time" content="2020-10-04T09:09:48.775Z">
<meta property="article:author" content="shanlu">
<meta property="article:tag" content="python">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://github.com/shanlu123/2020/09/27/python/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>数据结构 | shanlu的博客</title>
  






  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>
  <a href="https://github.com/shanlu123" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">shanlu的博客</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">三餐四季，冷暖自知</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/home" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-前端">

    <a href="/%E5%89%8D%E7%AB%AF/" rel="section"><i class="fa fa-cloud fa-fw"></i>前端</a>

  </li>
        <li class="menu-item menu-item-生活">

    <a href="/%E7%94%9F%E6%B4%BB/" rel="section"><i class="fa fa-user fa-fw"></i>生活</a>

  </li>
        <li class="menu-item menu-item-school">

    <a href="/school/" rel="section"><i class="fa fa-graduation-cap fa-fw"></i>学校</a>

  </li>
        <li class="menu-item menu-item-tree_hole">

    <a href="/tree_hole/" rel="section"><i class="fa fa-tree fa-fw"></i>树洞</a>

  </li>
        <li class="menu-item menu-item-photos">

    <a href="/photos/" rel="section"><i class="fa fa-camera fa-fw"></i>相册</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <section class="post-toc-wrap sidebar-panel">
          <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%97%E8%A1%A8"><span class="nav-number">1.</span> <span class="nav-text">列表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#del%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.</span> <span class="nav-text">del语句</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%85%83%E7%BB%84%E5%92%8C%E5%BA%8F%E5%88%97"><span class="nav-number">3.</span> <span class="nav-text">元组和序列</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%AF%E5%8F%98%E5%AF%B9%E8%B1%A1"><span class="nav-number">4.</span> <span class="nav-text">可变对象</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%9B%86%E5%90%88"><span class="nav-number">5.</span> <span class="nav-text">集合</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AD%97%E5%85%B8"><span class="nav-number">6.</span> <span class="nav-text">字典</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BE%AA%E7%8E%AF%E7%9A%84%E6%8A%80%E5%B7%A7"><span class="nav-number">7.</span> <span class="nav-text">循环的技巧</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B7%B1%E5%85%A5%E6%9D%A1%E4%BB%B6%E6%8E%A7%E5%88%B6"><span class="nav-number">8.</span> <span class="nav-text">深入条件控制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%AF%94%E8%BE%83%E5%BA%8F%E5%88%97%E5%92%8C%E5%85%B6%E4%BB%96%E7%B1%BB%E5%9E%8B"><span class="nav-number">9.</span> <span class="nav-text">比较序列和其他类型</span></a></li></ol></div>
      </section>
      <!--/noindex-->

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="shanlu"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">shanlu</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">127</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/shanlu123" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;shanlu123"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.cnblogs.com/shanlu0000/p/11478994.html" title="博客园 → https:&#x2F;&#x2F;www.cnblogs.com&#x2F;shanlu0000&#x2F;p&#x2F;11478994.html" rel="noopener" target="_blank"><i class="fa fa-blog fa-fw"></i>博客园</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:yourname@gmail.com" title="E-Mail → mailto:yourname@gmail.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/u/6662419816/home?wvr=5" title="微博 → https:&#x2F;&#x2F;weibo.com&#x2F;u&#x2F;6662419816&#x2F;home?wvr&#x3D;5" rel="noopener" target="_blank"><i class="fab fa-weibo fa-fw"></i>微博</a>
      </span>
  </div>


  <div class="links-of-blogroll animated">
    <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://www.npmjs.com/" title="https:&#x2F;&#x2F;www.npmjs.com&#x2F;" rel="noopener" target="_blank">npm</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://www.bilibili.com/" title="https:&#x2F;&#x2F;www.bilibili.com&#x2F;" rel="noopener" target="_blank">bilibili</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://stackoverflow.com/" title="https:&#x2F;&#x2F;stackoverflow.com&#x2F;" rel="noopener" target="_blank">stackoverflow</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://developers.weixin.qq.com/miniprogram/dev/component/" title="https:&#x2F;&#x2F;developers.weixin.qq.com&#x2F;miniprogram&#x2F;dev&#x2F;component&#x2F;" rel="noopener" target="_blank">小程序</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://www.ruanyifeng.com/blog/" title="http:&#x2F;&#x2F;www.ruanyifeng.com&#x2F;blog&#x2F;" rel="noopener" target="_blank">阮一峰</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://cn.aliyun.com/" title="https:&#x2F;&#x2F;cn.aliyun.com&#x2F;" rel="noopener" target="_blank">阿里云</a>
        </li>
    </ul>
  </div>

      </section>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">
      

      

    
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/shanlu123/2020/09/27/python/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="shanlu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="shanlu的博客">
    </span>

    
    
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          数据结构
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-09-27 00:00:00" itemprop="dateCreated datePublished" datetime="2020-09-27T00:00:00+08:00">2020-09-27</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2020-10-04 17:09:48" itemprop="dateModified" datetime="2020-10-04T17:09:48+08:00">2020-10-04</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/python/" itemprop="url" rel="index"><span itemprop="name">python</span></a>
        </span>
    </span>

  
    <span id="/2020/09/27/python/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" class="post-meta-item leancloud_visitors" data-flag-title="数据结构" title="阅读次数">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span class="leancloud-visitors-count"></span>
    </span>
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2020/09/27/python/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/09/27/python/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>24k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>22 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h3 id="列表"><a href="#列表" class="headerlink" title="列表"></a>列表</h3><ol>
<li><p>列表是python中的一个对象</p>
<ol start="2">
<li><p>数值只能保存一个单一的数据，列表中可以保存多个有序的数据</p>
</li>
<li><p>列表的创建：通过[] 创建</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">list = []  <span class="comment"># 创建了一个空列表</span></span><br><span class="line">print(list, type(list))  <span class="comment"># [] &lt;class &#x27;list&#x27;&gt;</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
</ol>
</li>
<li><p>一个列表中可以存储多个元素，也可以在创建列表时，指定列表中的元素</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="number">5</span>]  <span class="comment"># 创建一个包含5个元素的列表</span></span><br><span class="line">list = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>]  <span class="comment"># 创建了一个有5个元素的列表</span></span><br><span class="line">print(list)  <span class="comment"># [10, 20, 30, 40, 50]</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>列表中可以保存任意的对象</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="number">10</span>, <span class="string">&#x27;hello&#x27;</span>, <span class="literal">True</span>, <span class="literal">None</span>, [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], <span class="keyword">print</span>]</span><br><span class="line">print(list)  <span class="comment"># [10, &#x27;hello&#x27;, True, None, [1, 2, 3], &lt;built-in function print&gt;]</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>可以通过索引获取列表中的元素</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>]</span><br><span class="line">print(list[<span class="number">4</span>])  <span class="comment"># 50</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>获取列表的长度，len()</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>]</span><br><span class="line">print(len(list))  <span class="comment"># 5</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>+，将两个列表拼接为一个列表</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>] + [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]</span><br><span class="line">print(list)  <span class="comment"># [1, 2, 3, 4, 5, 6]</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>’ * ‘，将列表重复指定次数</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>] * <span class="number">2</span></span><br><span class="line">print(list)  <span class="comment"># [1, 2, 3, 1, 2, 3]</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>in 和 not in ，检查指定元素是否存在于列表中，返回True或false</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>]</span><br><span class="line"></span><br><span class="line">print(<span class="number">10</span> <span class="keyword">not</span> <span class="keyword">in</span> list)  <span class="comment"># False</span></span><br><span class="line">print(<span class="number">10</span> <span class="keyword">in</span> list)  <span class="comment"># True</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>min() 获取列表中最小值， max() 获取列表中最大值</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>]</span><br><span class="line">print(min(list), max(list))  <span class="comment"># 10 50</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>列表对象方法</p>
</li>
</ol>
<blockquote>
<p>list.append(x)：在列表的末尾添加一个元素</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">fruits = [<span class="string">&#x27;orange&#x27;</span>, <span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;pear&#x27;</span>, <span class="string">&#x27;banana&#x27;</span>, <span class="string">&#x27;kiwi&#x27;</span>, <span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;banana&#x27;</span>]</span><br><span class="line">fruits.append(<span class="string">&#x27;grape&#x27;</span>) </span><br><span class="line">print(fruits) <span class="comment">#[&#x27;orange&#x27;, &#x27;apple&#x27;, &#x27;pear&#x27;, &#x27;banana&#x27;, &#x27;kiwi&#x27;, &#x27;apple&#x27;, &#x27;banana&#x27;, &#x27;grape&#x27;]</span></span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>list.extend(iterable)：使用可迭代对象中的所有元素来扩展列表，相当于+=</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="string">&#x27;张三&#x27;</span>, <span class="string">&#x27;李四&#x27;</span>, <span class="string">&#x27;王五&#x27;</span>, <span class="string">&#x27;李华&#x27;</span>]</span><br><span class="line">list.extend([<span class="string">&#x27;刘晨&#x27;</span>,<span class="string">&#x27;王立&#x27;</span>]) </span><br><span class="line"><span class="comment">#[&#x27;张三&#x27;, &#x27;李四&#x27;, &#x27;王五&#x27;, &#x27;李华&#x27;, &#x27;刘晨&#x27;, &#x27;王立&#x27;]</span></span><br><span class="line"></span><br><span class="line">list += [<span class="string">&#x27;王敏&#x27;</span>,<span class="string">&#x27;朱丽&#x27;</span>] </span><br><span class="line"><span class="comment">#[&#x27;张三&#x27;, &#x27;李四&#x27;, &#x27;王五&#x27;, &#x27;李华&#x27;, &#x27;刘晨&#x27;, &#x27;王立&#x27;, &#x27;王敏&#x27;, &#x27;朱丽&#x27;]</span></span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>list.inert(i，x)：在给定的位置插入一个元素，第一个参数是要插入的元素的索引，第二个参数是要插入的元素</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="string">&#x27;张三&#x27;</span>, <span class="string">&#x27;李四&#x27;</span>, <span class="string">&#x27;王五&#x27;</span>, <span class="string">&#x27;李华&#x27;</span>]</span><br><span class="line">list.insert(<span class="number">2</span>, <span class="string">&#x27;Jack&#x27;</span>)</span><br><span class="line">print(list)  <span class="comment"># [&#x27;张三&#x27;, &#x27;李四&#x27;, &#x27;Jack&#x27;, &#x27;王五&#x27;, &#x27;李华&#x27;]</span></span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>list.remove(x)：移除列表中第一个值为x的元素，如果没有这样的元素，则抛出ValueError异常</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="string">&#x27;张三&#x27;</span>, <span class="string">&#x27;李四&#x27;</span>, <span class="string">&#x27;王五&#x27;</span>, <span class="string">&#x27;李华&#x27;</span>,<span class="string">&#x27;张三&#x27;</span>]</span><br><span class="line">list.remove(<span class="string">&#x27;张三&#x27;</span>)</span><br><span class="line">print(list) <span class="comment">#[&#x27;李四&#x27;, &#x27;王五&#x27;, &#x27;李华&#x27;, &#x27;张三&#x27;]</span></span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>list.pop(【x】)：删除列表中给定位置的元素并返回它，如果没有给定位置，a.pop() 将会删除并返回列表的最后一个元素。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="string">&#x27;张三&#x27;</span>, <span class="string">&#x27;李四&#x27;</span>, <span class="string">&#x27;王五&#x27;</span>, <span class="string">&#x27;李华&#x27;</span>]</span><br><span class="line">list.pop(<span class="number">2</span>)  <span class="comment"># 删除索引为2的元素</span></span><br><span class="line">print(list)  <span class="comment"># [&#x27;张三&#x27;, &#x27;李四&#x27;, &#x27;李华&#x27;]</span></span><br><span class="line"></span><br><span class="line">fruits = [<span class="string">&#x27;orange&#x27;</span>, <span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;pear&#x27;</span>, <span class="string">&#x27;banana&#x27;</span>, <span class="string">&#x27;kiwi&#x27;</span>, <span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;banana&#x27;</span>]</span><br><span class="line">print(fruits.pop())  <span class="comment"># banana</span></span><br><span class="line">print(fruits)  <span class="comment"># [&#x27;orange&#x27;, &#x27;apple&#x27;, &#x27;pear&#x27;, &#x27;banana&#x27;, &#x27;kiwi&#x27;, &#x27;apple&#x27;]</span></span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>list.clear()：移除列表中的所有元素</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="string">&#x27;张三&#x27;</span>, <span class="string">&#x27;李四&#x27;</span>, <span class="string">&#x27;王五&#x27;</span>, <span class="string">&#x27;李华&#x27;</span>]</span><br><span class="line">list.clear()</span><br><span class="line">print(list)  <span class="comment"># []</span></span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>list.index(x【，start [,end] 】)：返回元素在列表中第一次出现的索引，如果没有这样的元素将会抛出ValueError异常，可选参数 start 和 end 是切片符号，用于将搜索限制为列表的特定子序列。返回的索引是相对于整个序列的开始计算的，而不是start参数。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">fruits = [<span class="string">&#x27;orange&#x27;</span>, <span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;pear&#x27;</span>, <span class="string">&#x27;banana&#x27;</span>, <span class="string">&#x27;kiwi&#x27;</span>, <span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;banana&#x27;</span>]</span><br><span class="line">print(fruits.index(<span class="string">&#x27;banana&#x27;</span>))  <span class="comment"># 3</span></span><br><span class="line">print(fruits.index(<span class="string">&#x27;banana&#x27;</span>, <span class="number">4</span>)) <span class="comment">#6</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># index()的第二个参数，表示查找的起始位置 ， 第三个参数，表示查找的结束位置</span></span><br><span class="line">list = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>]</span><br><span class="line">print(list.index(<span class="number">10</span>, <span class="number">0</span>, <span class="number">4</span>))  <span class="comment"># 0</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 如果要获取列表中没有的元素，会抛出异常</span></span><br><span class="line">print(list.index(<span class="number">10</span>, <span class="number">0</span>, <span class="number">4</span>))  <span class="comment"># ValueError: 10 is not in list</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#注意对于不可变序列，无法通过索引来修改</span></span><br><span class="line">s = <span class="string">&#x27;hello&#x27;</span></span><br><span class="line"><span class="comment"># s[1] = &#x27;a&#x27; </span></span><br><span class="line"><span class="comment"># TypeError: &#x27;str&#x27; object does not support item assignment</span></span><br><span class="line"></span><br><span class="line">s = list(s)  <span class="comment"># 可以通过 list() 函数将其转换为列表进行修改</span></span><br><span class="line">s[<span class="number">1</span>] = <span class="string">&#x27;a&#x27;</span></span><br><span class="line">print(s)  <span class="comment"># [&#x27;h&#x27;, &#x27;a&#x27;, &#x27;l&#x27;, &#x27;l&#x27;, &#x27;o&#x27;]</span></span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>list.count(x)：返回x在列表中出现的次数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">fruits = [<span class="string">&#x27;orange&#x27;</span>, <span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;pear&#x27;</span>, <span class="string">&#x27;banana&#x27;</span>, <span class="string">&#x27;kiwi&#x27;</span>, <span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;banana&#x27;</span>]</span><br><span class="line">print(fruits.count(<span class="string">&#x27;apple&#x27;</span>))  <span class="comment">#2</span></span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>list.sort(key=None，reverse=False)：对列表转给你的元素进行排序，默认是升序排列，参数可用于自定义排序，reverse=True时就是降序排列</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">fruits = [<span class="string">&#x27;orange&#x27;</span>, <span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;pear&#x27;</span>, <span class="string">&#x27;banana&#x27;</span>, <span class="string">&#x27;kiwi&#x27;</span>, <span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;banana&#x27;</span>]</span><br><span class="line">fruits.sort()</span><br><span class="line">print(fruits) </span><br><span class="line"><span class="comment"># [&#x27;apple&#x27;, &#x27;apple&#x27;, &#x27;banana&#x27;, &#x27;banana&#x27;, &#x27;kiwi&#x27;, &#x27;orange&#x27;, &#x27;pear&#x27;]</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">list = list(<span class="string">&#x27;asnbdnbasdabd&#x27;</span>)</span><br><span class="line">list.sort(reverse=<span class="literal">True</span>)</span><br><span class="line">print(list)</span><br><span class="line"><span class="comment"># [&#x27;s&#x27;, &#x27;s&#x27;, &#x27;n&#x27;, &#x27;n&#x27;, &#x27;d&#x27;, &#x27;d&#x27;, &#x27;d&#x27;, &#x27;b&#x27;, &#x27;b&#x27;, &#x27;b&#x27;, &#x27;a&#x27;, &#x27;a&#x27;, &#x27;a&#x27;]</span></span><br></pre></td></tr></table></figure>

<p>注意，并非所有的数据都可以排序或比较，如[None，’hello’，10] 就不可以排序，因为整数不能与字符串比较，而None不能与其他类型比较</p>
</blockquote>
<blockquote>
<p>list.reverse()：翻转列表中的元素</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">fruits = [<span class="string">&#x27;orange&#x27;</span>, <span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;pear&#x27;</span>, <span class="string">&#x27;banana&#x27;</span>, <span class="string">&#x27;kiwi&#x27;</span>, <span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;banana&#x27;</span>]</span><br><span class="line">fruits.reverse()</span><br><span class="line">print(fruits)</span><br><span class="line"><span class="comment">#[&#x27;banana&#x27;, &#x27;apple&#x27;, &#x27;kiwi&#x27;, &#x27;banana&#x27;, &#x27;pear&#x27;, &#x27;apple&#x27;, &#x27;orange&#x27;]</span></span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>list.copy()：返回列表中的一个浅拷贝</p>
</blockquote>
<blockquote>
<p>注意，像 insert，remove，sort等方法，只修改列表，没有返回值(返回默认值是None)</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">fruits = [<span class="string">&#x27;orange&#x27;</span>, <span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;pear&#x27;</span>, <span class="string">&#x27;banana&#x27;</span>, <span class="string">&#x27;kiwi&#x27;</span>, <span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;banana&#x27;</span>]</span><br><span class="line">print(fruits.sort()) <span class="comment"># None</span></span><br></pre></td></tr></table></figure>
</blockquote>
<ol start="13">
<li><p>遍历列表</p>
<ul>
<li><p>通过while循环遍历</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="string">&#x27;张三&#x27;</span>, <span class="string">&#x27;李四&#x27;</span>, <span class="string">&#x27;王五&#x27;</span>, <span class="string">&#x27;李华&#x27;</span>]</span><br><span class="line">i = <span class="number">0</span></span><br><span class="line"><span class="keyword">while</span> i &lt; len(list):</span><br><span class="line">    print(list[i])</span><br><span class="line">    i += <span class="number">1</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>通过for循环遍历</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="string">&#x27;张三&#x27;</span>, <span class="string">&#x27;李四&#x27;</span>, <span class="string">&#x27;王五&#x27;</span>, <span class="string">&#x27;李华&#x27;</span>]</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> list :</span><br><span class="line">    print(i)</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>range()</p>
<ul>
<li><p>是一个函数，可以用来生成自然数的序列</p>
</li>
<li><p>该函数需要三个参数，起始位置，结束位置，步长，都可以省略</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">r1 = range(<span class="number">5</span>) </span><br><span class="line">print(<span class="string">&#x27;r1=&#x27;</span>, r1)  <span class="comment"># r1=range(0, 5), 即[0,1,2,3,4]</span></span><br><span class="line">r2 = range(<span class="number">0</span>, <span class="number">10</span>, <span class="number">2</span>)</span><br><span class="line">r3 = range(<span class="number">10</span>, <span class="number">0</span>, <span class="number">-1</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>经常配合for循环使用，通过range() 可以创建一个指定次数的for循环</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">5</span>):</span><br><span class="line">    print(i, end=<span class="string">&quot; &quot;</span>)  <span class="comment"># 0 1 2 3 4</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> s <span class="keyword">in</span> <span class="string">&#x27;hello&#x27;</span>:</span><br><span class="line">    print(s, end=<span class="string">&quot; &quot;</span>)  <span class="comment"># h e l l o</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>修改列表操作</p>
<ul>
<li><p>直接通过索引修改列表中的元素</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>]</span><br><span class="line">list[<span class="number">0</span>] = <span class="string">&#x27;hello&#x27;</span></span><br><span class="line">list[<span class="number">2</span>] = <span class="string">&#x27;world&#x27;</span></span><br><span class="line">print(list)  <span class="comment"># [&#x27;hello&#x27;, 20, &#x27;world&#x27;, 40, 50]</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>通过切片修改</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>]</span><br><span class="line">list[<span class="number">0</span>:<span class="number">2</span>] = [<span class="string">&#x27;hello&#x27;</span>, <span class="string">&#x27;wrold&#x27;</span>]  <span class="comment"># 使用新的元素替换旧元素, 注意在给切片进行赋值时，只能使用序列</span></span><br><span class="line"><span class="comment"># [&#x27;hello&#x27;, &#x27;wrold&#x27;, 30, 40, 50]</span></span><br><span class="line">list[<span class="number">0</span>:<span class="number">0</span>] = [<span class="string">&#x27;abc&#x27;</span>]  <span class="comment"># 向索引为0的位置插入元素</span></span><br><span class="line"><span class="comment"># [&#x27;abc&#x27;, &#x27;hello&#x27;, &#x27;wrold&#x27;, 30, 40, 50]</span></span><br><span class="line"></span><br><span class="line">list = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>]</span><br><span class="line">list[::<span class="number">2</span>] = [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>]  <span class="comment"># 当设置了步长时，序列中元素的个数必须和切片中元素的个数一致</span></span><br><span class="line">print(list)  <span class="comment"># [&#x27;a&#x27;, 20, &#x27;b&#x27;, 40, &#x27;c&#x27;]</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>通过 del 删除列表中的元素</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>]</span><br><span class="line"><span class="keyword">del</span> list[<span class="number">2</span>]  <span class="comment"># 删除索引为2的元素</span></span><br><span class="line">print(list)  <span class="comment"># [10, 20, 40, 50]</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>通过切片删除元素    </p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>]</span><br><span class="line"><span class="keyword">del</span> list[<span class="number">0</span>:<span class="number">2</span>]  <span class="comment"># [30, 40, 50]</span></span><br><span class="line"><span class="keyword">del</span> list[::<span class="number">2</span>]  <span class="comment"># [40]</span></span><br><span class="line"></span><br><span class="line">list = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>]</span><br><span class="line">list[<span class="number">1</span>:<span class="number">3</span>] = []  <span class="comment"># [10, 40, 50]</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>列表的切片操作</p>
<ul>
<li><p>语法：列表[起始:结束] </p>
</li>
<li><p>通过切片获取元素时，会包括起始位置的元素，不会包括结束位置的元素，左闭右开</p>
</li>
<li><p>切片指从现有列表中获取子列表，返回一个新的列表，不会影响原来的列表</p>
</li>
<li><p>索引是负数，从后往前获取元素，-1表示倒数第一个</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="string">&#x27;张三&#x27;</span>, <span class="string">&#x27;李四&#x27;</span>, <span class="string">&#x27;王五&#x27;</span>, <span class="string">&#x27;李华&#x27;</span>, <span class="string">&#x27;李明&#x27;</span>]</span><br><span class="line">print(list[<span class="number">-2</span>])  <span class="comment"># 李华</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>起始位置和结束位置可以省略不写，全部省略时，相当于创建了一个列表的副本</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="string">&#x27;张三&#x27;</span>, <span class="string">&#x27;李四&#x27;</span>, <span class="string">&#x27;王五&#x27;</span>, <span class="string">&#x27;李华&#x27;</span>, <span class="string">&#x27;李明&#x27;</span>]</span><br><span class="line">print(list[<span class="number">1</span>:])  <span class="comment"># [&#x27;李四&#x27;, &#x27;王五&#x27;, &#x27;李华&#x27;, &#x27;李明&#x27;]</span></span><br><span class="line">print(list[:<span class="number">3</span>])  <span class="comment"># [&#x27;张三&#x27;, &#x27;李四&#x27;, &#x27;王五&#x27;]</span></span><br><span class="line">print(list[:])  <span class="comment"># [&#x27;张三&#x27;, &#x27;李四&#x27;, &#x27;王五&#x27;, &#x27;李华&#x27;, &#x27;李明&#x27;]</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>步长：</p>
<ul>
<li><p>语法：列表[起始:结束:步长] </p>
</li>
<li><p>表示每次获取元素的间隔，默认值是1，隔一个取一个，</p>
</li>
<li><p>步长可以是负数，负数则从后往前取，步长不能是0</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="string">&#x27;张三&#x27;</span>, <span class="string">&#x27;李四&#x27;</span>, <span class="string">&#x27;王五&#x27;</span>, <span class="string">&#x27;李华&#x27;</span>, <span class="string">&#x27;李明&#x27;</span>]</span><br><span class="line">print(list[<span class="number">0</span>::<span class="number">3</span>])  <span class="comment"># [&#x27;张三&#x27;, &#x27;李华&#x27;]</span></span><br><span class="line">print(list[::<span class="number">-1</span>])  <span class="comment"># [&#x27;李明&#x27;, &#x27;李华&#x27;, &#x27;王五&#x27;, &#x27;李四&#x27;, &#x27;张三&#x27;]</span></span><br><span class="line">print(list[::<span class="number">0</span>])  <span class="comment"># ValueError: slice step cannot be zero</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
</li>
<li><p>列表作为栈使用</p>
<blockquote>
<p>列表方法使得列表作为堆栈非常容易，最后一个插入，最先取出(“先进后出”)，要添加一个元素到堆栈的顶端，使用append()，要从堆栈顶部取出一个元素，使用 pop()，不用指定索引。</p>
</blockquote>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">stack = [<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]</span><br><span class="line">stack.append(<span class="number">6</span>)</span><br><span class="line">stack.append(<span class="number">7</span>)</span><br><span class="line">print(stack)  <span class="comment"># [3, 4, 5, 6, 7]</span></span><br><span class="line">stack.pop()</span><br><span class="line">stack.pop()</span><br><span class="line">print(stack)  <span class="comment"># [3, 4, 5]</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>列表作为队列使用</p>
<blockquote>
<p>列表也可用作队列，先添加的元素被最先取出（“先进先出”）。然而列表用作这个目的相当低效，因为在列表的末尾添加和弹出元素非常快，但是在列表的开头插入或弹出元素却很慢，因为所有的其他元素都必须移动一位。</p>
<p>若要实现一个队列，可使用 collection.deque ，它被设计成可以快速地从两端添加或弹出元素。</p>
</blockquote>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> collections <span class="keyword">import</span> deque</span><br><span class="line"></span><br><span class="line">queue = deque([<span class="string">&quot;Eric&quot;</span>, <span class="string">&quot;John&quot;</span>, <span class="string">&quot;Michael&quot;</span>])</span><br><span class="line">queue.append(<span class="string">&quot;Terry&quot;</span>)</span><br><span class="line">queue.append(<span class="string">&quot;Graham&quot;</span>)</span><br><span class="line">print(queue)  <span class="comment"># deque([&#x27;Eric&#x27;, &#x27;John&#x27;, &#x27;Michael&#x27;, &#x27;Terry&#x27;, &#x27;Graham&#x27;])</span></span><br><span class="line">print(queue.popleft())  <span class="comment"># Eric</span></span><br><span class="line">print(queue)  <span class="comment"># deque([&#x27;John&#x27;, &#x27;Michael&#x27;, &#x27;Terry&#x27;, &#x27;Graham&#x27;])</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>列表推导式</p>
<blockquote>
<p>列表推导式提供了一个更简单的方法创建列表，常见的用法是把某种操作应用于序列或可迭代对象的每个元素上，然后实验其结果来创建列表，或者通过满足某些特定条件元素来创建子序列</p>
<p>如，创建一个平方列表</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">squares = []</span><br><span class="line"><span class="keyword">for</span> item <span class="keyword">in</span> range(<span class="number">10</span>):</span><br><span class="line">    squares.append(item ** <span class="number">2</span>)</span><br><span class="line">print(squares) <span class="comment">#[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]</span></span><br></pre></td></tr></table></figure>

<p>这里创建(或被重写) 的 item 变量在for循环后仍然存在，我们可以计算平方列表的值而不会产生任何副作用。</p>
<p>等价于：squares = list(map (lambda x: x**2,  range(10)))</p>
<p>也等价于：squares=[x**2 for x in range(10)]</p>
</blockquote>
<blockquote>
<p>列表推导式的结构是由一对方括号所包含的以下内容：</p>
<p>一个表达式，后跟一个for子句，然后是0个或多个for或if子句。</p>
<p>其结果将是一个新列表，有表达式依据后面的 for 和 if 子句的内容进行求值计算得出。</p>
<p>如：使用列表推导式将两个列表中不相等的元素组合起来</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">v=[(x, y) <span class="keyword">for</span> x <span class="keyword">in</span> [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>] <span class="keyword">for</span> y <span class="keyword">in</span> [<span class="number">3</span>,<span class="number">1</span>,<span class="number">4</span>] <span class="keyword">if</span> x != y]</span><br><span class="line">print(v) <span class="comment">#[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]</span></span><br></pre></td></tr></table></figure>

<p>等价于：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">v=[]</span><br><span class="line"><span class="keyword">for</span> x <span class="keyword">in</span> [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line">	<span class="keyword">for</span> y <span class="keyword">in</span> [<span class="number">3</span>,<span class="number">1</span>,<span class="number">4</span>]:</span><br><span class="line">		<span class="keyword">if</span> x!=y</span><br><span class="line">			v.append((x,y))</span><br><span class="line">print(v)</span><br></pre></td></tr></table></figure>

<p>注意这这两种方式中，for 和 if 的顺序是相同的</p>
</blockquote>
<blockquote>
<p>如果表达式是一个元组，必须加上括号</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">vec = [<span class="number">-4</span>, <span class="number">-2</span>, <span class="number">0</span>, <span class="number">2</span>, <span class="number">4</span>]</span><br><span class="line">new_vec0 = [x * <span class="number">2</span> <span class="keyword">for</span> x <span class="keyword">in</span> vec]</span><br><span class="line">print(new_vec0)  <span class="comment"># [-8, -4, 0, 4, 8]</span></span><br><span class="line">new_vec1 = [(x, x * <span class="number">2</span>) <span class="keyword">for</span> x <span class="keyword">in</span> vec]</span><br><span class="line">print(new_vec1)  <span class="comment"># [(-4, -8), (-2, -4), (0, 0), (2, 4), (4, 8)]</span></span><br><span class="line"></span><br><span class="line">new_vec2 = [x <span class="keyword">for</span> x <span class="keyword">in</span> vec <span class="keyword">if</span> x &gt;= <span class="number">0</span>]</span><br><span class="line">print(new_vec2)  <span class="comment"># [0, 2, 4]</span></span><br><span class="line"></span><br><span class="line">new_vec3 = [abs(x) <span class="keyword">for</span> x <span class="keyword">in</span> vec]</span><br><span class="line">print(new_vec3)  <span class="comment"># [4, 2, 0, 2, 4]</span></span><br><span class="line"></span><br><span class="line">fruits = [<span class="string">&#x27;  banana&#x27;</span>, <span class="string">&#x27;loganberry   &#x27;</span>, <span class="string">&#x27;  possion fruit  &#x27;</span>]</span><br><span class="line">new_fruits = [item.strip() <span class="keyword">for</span> item <span class="keyword">in</span> fruits]</span><br><span class="line">print(new_fruits)  <span class="comment"># [&#x27;banana&#x27;, &#x27;loganberry&#x27;, &#x27;possion fruit&#x27;]</span></span><br><span class="line"></span><br><span class="line">num = [[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>], [<span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>]]</span><br><span class="line">new_num = [i <span class="keyword">for</span> j <span class="keyword">in</span> num <span class="keyword">for</span> i <span class="keyword">in</span> j]</span><br><span class="line">print(new_num)  <span class="comment"># [1, 2, 3, 4, 5, 6, 7, 8, 9]</span></span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>列表推导式可以使用复杂的表达式和嵌套函数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> math <span class="keyword">import</span> pi</span><br><span class="line"></span><br><span class="line">a = [str(round(pi, i)) <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">1</span>, <span class="number">6</span>)]</span><br><span class="line">print(a)  <span class="comment"># [&#x27;3.1&#x27;, &#x27;3.14&#x27;, &#x27;3.142&#x27;, &#x27;3.1416&#x27;, &#x27;3.14159&#x27;]</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>嵌套的列表推导式</p>
<blockquote>
<p>列表推导式中的初始表达式可以是任何表达式，包括另一个列表推导式</p>
<p>如下面这个 3x4 的矩阵，它由3个长度为4的列表组成</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">juzhen=[</span><br><span class="line">	[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>],</span><br><span class="line">	[<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>],</span><br><span class="line">	[<span class="number">9</span>,<span class="number">10</span>,<span class="number">11</span>,<span class="number">12</span>]</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<p>下面的列表推导式将交换其行与列</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">new_juzhen=[[row[i] <span class="keyword">for</span> row <span class="keyword">in</span> juzhen] <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">4</span>)]</span><br><span class="line">print(new_juzhen)</span><br><span class="line"></span><br><span class="line"><span class="comment"># [[1,5,9],[2,6,10],[3,7,11],[4,8,]]</span></span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>嵌套的列表推导式是基于跟随其后的for进行求值的</p>
<p>上面的例子等价于：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">arr1 = []</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">4</span>):</span><br><span class="line">    arr1.append([row[i] <span class="keyword">for</span> row <span class="keyword">in</span> juzhen])</span><br><span class="line">print(arr1)</span><br><span class="line"></span><br><span class="line"><span class="comment"># [[1,5,9],[2,6,10],[3,7,11],[4,8,]]</span></span><br></pre></td></tr></table></figure>

<p>也等价于：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">arr2 = []</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">4</span>):</span><br><span class="line">    arr2_row = []</span><br><span class="line">    <span class="keyword">for</span> row <span class="keyword">in</span> juzhen:</span><br><span class="line">        arr2_row.append(row[i])</span><br><span class="line">    arr2.append(arr2_row)</span><br><span class="line">print(arr2)</span><br><span class="line"></span><br><span class="line"><span class="comment"># [[1,5,9],[2,6,10],[3,7,11],[4,8,]]</span></span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>实际应用中，更多私用内置函数去组成复杂的流程语句，zip() 函数将会很好的处理这种情况</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">arr3=list(zip(*juzhen))</span><br><span class="line">print(arr3)</span><br></pre></td></tr></table></figure>
</blockquote>
</li>
</ol>
<h3 id="del语句"><a href="#del语句" class="headerlink" title="del语句"></a>del语句</h3><ul>
<li><p>可以从列表按照给定的索引而不是值来移除一个元素，它不同于会返回一个值的pop() 方法。</p>
</li>
<li><p>del语句也可以用来从列表中移除切片或者清空整个列表</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">a = [<span class="number">-1</span>, <span class="number">1</span>, <span class="number">66</span>, <span class="number">25</span>, <span class="number">333</span>, <span class="number">333</span>, <span class="number">1234</span>, <span class="number">5</span>]</span><br><span class="line"><span class="keyword">del</span> a[<span class="number">0</span>]</span><br><span class="line">print(a)  <span class="comment"># [1, 66, 25, 333, 333, 1234, 5]</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">del</span> a[<span class="number">2</span>:<span class="number">4</span>]</span><br><span class="line">print(a)  <span class="comment"># [1, 66, 333, 1234, 5]</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">del</span> a[:]</span><br><span class="line">print(a)  <span class="comment"># []</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>del 也可删除整个变量</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">del</span> a</span><br><span class="line">print(a)  <span class="comment"># name &#x27;a&#x27; is not defined</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="元组和序列"><a href="#元组和序列" class="headerlink" title="元组和序列"></a>元组和序列</h3><ol>
<li><p>列表和字符串有很多共性，例如索引和切片等，它们是序列数据类型（list，tuple，range) 中的两种。</p>
</li>
<li><p>随着python语言的发展，其他的序列类型也会被加入其中，如其中一种标准序列类型：元组（ tuple ）</p>
</li>
<li><p>创建元组，使用() 创建元组</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">t = ()  <span class="comment"># 创建了一个空元组</span></span><br><span class="line">print(t, type(t))  <span class="comment"># () &lt;class &#x27;tuple&#x27;&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 含有一个元素的元组可以通过在这个元组后添加一个逗号来构建，因为圆括号中只有一个值的话不够明确</span></span><br><span class="line">  single = <span class="string">&#x27;hello&#x27;</span>,</span><br><span class="line">  print(single)  <span class="comment"># (&#x27;hello&#x27;,)</span></span><br><span class="line">  print(len(single))  <span class="comment"># 1</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>元组在输出时总是被圆括号包围的，以便正确表示嵌套元组</p>
</li>
<li><p>元组是一个不可变的序列，不能尝试为元组中的元素重新赋值，一个元组由几个被逗号隔开的值组成</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">t = (<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>)  <span class="comment"># 创建了一个5个元素的元组</span></span><br><span class="line">t[<span class="number">3</span>] = <span class="number">10</span>  </span><br><span class="line"><span class="comment"># TypeError: &#x27;tuple&#x27; object does not support item assignment</span></span><br><span class="line">print(t[<span class="number">3</span>])</span><br></pre></td></tr></table></figure>
</li>
<li><p>当元组不是空元组时，括号可以省略，不过经常必须有，如果这个元组是一个更大的表达式的一部分的话</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">t = <span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span></span><br><span class="line">print(t)  <span class="comment"># (10, 20, 30, 40)</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>解包：</p>
<ul>
<li><p>语句 t=12345,54321,’hello!’ 是元组打包的一个例子，值 12345,54321,’hello!’ 被打包进元组，其逆操作也是允许的</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">x,y,z=t</span><br></pre></td></tr></table></figure>

<p> 这被称为序列解包。解包操作的等号右侧可以是任何序列，序列解包的要求是等号左侧的变量数与右侧序列里所含的元素数相同。多重赋值也只是元组打包和序列解包的组合。</p>
</li>
<li><p>解包：把元组中的每一个元素都赋值给一个变量</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">t = <span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span></span><br><span class="line">a, b, c, d = t</span><br><span class="line">print(<span class="string">f&#x27;a=<span class="subst">&#123;a&#125;</span>,b=<span class="subst">&#123;b&#125;</span>,c=<span class="subst">&#123;c&#125;</span>,d=<span class="subst">&#123;d&#125;</span>&#x27;</span>)</span><br><span class="line"><span class="comment"># a=10,b=20,c=30,d=40</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>利用解包的特性很容易交换两个变量</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">10</span></span><br><span class="line">b = <span class="number">20</span></span><br><span class="line">a, b = b, a</span><br><span class="line"></span><br><span class="line">print(a, b)  <span class="comment"># 20 10</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>在对元组进行解包时，变量的数量必须和元组中元素数量一直，也可以在变量前添加 “ * ”，会获取元组中所有剩余元素，注意不能同时出现两个或以上的*</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">t = <span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span></span><br><span class="line">a, b, *c = t  <span class="comment"># a=10,b=20,c=[30, 40]</span></span><br><span class="line">a, *b, c = t  <span class="comment"># s=10,b=[20, 30],c=40</span></span><br><span class="line">*a, b, c = t  <span class="comment"># s=[10, 20],b=30,c=40</span></span><br><span class="line"></span><br><span class="line">a, b, *c = <span class="string">&#x27;hello&#x27;</span></span><br><span class="line">print(<span class="string">f&#x27;a=<span class="subst">&#123;a&#125;</span>,b=<span class="subst">&#123;b&#125;</span>,c=<span class="subst">&#123;c&#125;</span>&#x27;</span>)  <span class="comment"># a=h,b=e,c=[&#x27;l&#x27;, &#x27;l&#x27;, &#x27;o&#x27;]</span></span><br><span class="line"><span class="comment"># 不能同时出现两个或以上的*变量</span></span><br><span class="line"><span class="comment"># *a , *b , c = t SyntaxError: two starred expressions in assignment</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>序列都可以解包</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">t = <span class="number">12345</span>, <span class="number">54321</span>, <span class="string">&#x27;hello!&#x27;</span></span><br><span class="line">print(t[<span class="number">0</span>])  <span class="comment"># 12345</span></span><br><span class="line">print(t)  <span class="comment"># (12345, 54321, &#x27;hello!&#x27;)</span></span><br><span class="line"></span><br><span class="line">u = t, (<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>)</span><br><span class="line">print(u)  <span class="comment"># ((12345, 54321, &#x27;hello!&#x27;), (1, 2, 3, 4, 5))</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">v = ([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], [<span class="number">3</span>, <span class="number">2</span>, <span class="number">1</span>])</span><br><span class="line">print(v)  <span class="comment"># ([1, 2, 3], [3, 2, 1])</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>元组可能看起来和列表很像，但它们通常在不同的场景被使用，并有着不同的用途</p>
<ul>
<li>元组是 immutable，其序列通常包含不同种类元素，并且通过解包或者索引来访问，如果是namedtuples的话甚至可以通过属性访问</li>
<li>列表时 mutable，列表中的元素一般是同种类型的，通过迭代访问</li>
</ul>
</li>
</ol>
<h3 id="可变对象"><a href="#可变对象" class="headerlink" title="可变对象"></a>可变对象</h3><ol>
<li><p>对象的value可变，列表就是一个可变对象</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line">print(<span class="string">&#x27;修改前：&#x27;</span>, a, id(a))  <span class="comment"># 修改前： [1, 2, 3] 1733767585728</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 通过索引修改对象</span></span><br><span class="line">a[<span class="number">0</span>] = <span class="number">10</span></span><br><span class="line">print(<span class="string">&#x27;修改后：&#x27;</span>, a, id(a))  <span class="comment"># 修改后： [10, 2, 3] 1733767585728</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 为变量重新赋值</span></span><br><span class="line">a = [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]</span><br><span class="line">print(<span class="string">&#x27;修改后：&#x27;</span>, a, id(a))  <span class="comment"># 修改后： [4, 5, 6] 2623572692544</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>is 和 ==</p>
<ul>
<li><p>== 比较的是对象的value是否相等</p>
</li>
<li><p>is 比较对象的id是否相同，是否是同一个对象，is 更严格</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line">b = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line">print(a == b)  <span class="comment"># a和b的值相等，使用==会返回True</span></span><br><span class="line">print(a <span class="keyword">is</span> b)  <span class="comment"># a和b不是同一个对象，内存地址不同，使用is会返回False</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
</li>
</ol>
<h3 id="集合"><a href="#集合" class="headerlink" title="集合"></a>集合</h3><ol>
<li><p>花括号或 set() 函数可以用来创建集合，注意，创建一个空集合只能用set() 而不能用 {}，因为后者是创建一个空字典。set() 还可以将序列和字典转换为集合</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">basket = &#123;<span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;orange&#x27;</span>, <span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;pear&#x27;</span>, <span class="string">&#x27;orange&#x27;</span>, <span class="string">&#x27;banana&#x27;</span>&#125;</span><br><span class="line">print(basket)  <span class="comment"># &#123;&#x27;banana&#x27;, &#x27;pear&#x27;, &#x27;orange&#x27;, &#x27;apple&#x27;&#125;</span></span><br><span class="line"></span><br><span class="line">s1 = set()  <span class="comment"># 空集合</span></span><br><span class="line">s2 = set([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>])</span><br><span class="line">s3 = set(<span class="string">&#x27;hello&#x27;</span>)</span><br><span class="line">s4 = set(&#123;<span class="string">&#x27;a&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;b&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;c&#x27;</span>: <span class="number">3</span>&#125;)  <span class="comment"># 使用set()将字典转换为集合时，只会包含字典中的键</span></span><br><span class="line">print(<span class="string">f&#x27;s1=<span class="subst">&#123;s1&#125;</span>,s2=<span class="subst">&#123;s2&#125;</span>,s3=<span class="subst">&#123;s3&#125;</span>,s4=<span class="subst">&#123;s4&#125;</span>&#x27;</span>)</span><br><span class="line"><span class="comment"># s1=set(),s2=&#123;1, 2, 3, 4, 5&#125;,s3=&#123;&#x27;e&#x27;, &#x27;l&#x27;, &#x27;o&#x27;, &#x27;h&#x27;&#125;,s4=&#123;&#x27;b&#x27;, &#x27;a&#x27;, &#x27;c&#x27;&#125;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>基本用法包括成员检测和消除重复元素</p>
</li>
<li><p>集合对象也支持 联合，交集，差集，对称差分等数学运算</p>
</li>
<li><p>集合不能通过索引操作，可以转换为列表操作</p>
</li>
<li><p>in  和 not in 检查集合中的元素，len() 获取集合中元素的数量，去重</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">s = &#123;<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span>&#125;</span><br><span class="line">print(<span class="string">&#x27;c&#x27;</span> <span class="keyword">in</span> s)  <span class="comment"># False</span></span><br><span class="line">print(len(s))  <span class="comment"># 5</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>add() 向集合中添加元素，没有返回值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">s = &#123;<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span>&#125;</span><br><span class="line">s.add(<span class="number">10</span>)</span><br><span class="line">print(s)  <span class="comment"># &#123;1, 2, 3, &#x27;b&#x27;, 10, &#x27;a&#x27;&#125;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>update（）将一个集合中的元素添加到当前集合中，可以传递序列或字典作为参数，字典只会使用键</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">s = &#123;<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span>&#125;</span><br><span class="line">s.update(set(<span class="string">&#x27;hex&#x27;</span>))  <span class="comment"># &#123;1, 2, 3, &#x27;e&#x27;, &#x27;h&#x27;, &#x27;a&#x27;, &#x27;x&#x27;, &#x27;b&#x27;&#125;</span></span><br><span class="line">s.update((<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>))  <span class="comment"># &#123;1, 2, 3, &#x27;e&#x27;, 40, 10, &#x27;h&#x27;, &#x27;a&#x27;, 50, 20, &#x27;x&#x27;, &#x27;b&#x27;, 30&#125;</span></span><br><span class="line">s.update(</span><br><span class="line">    &#123;<span class="number">10</span>: <span class="string">&#x27;ab&#x27;</span>, <span class="number">20</span>: <span class="string">&#x27;bc&#x27;</span>, <span class="number">100</span>: <span class="string">&#x27;cd&#x27;</span>&#125;)  <span class="comment"># &#123;1, 2, 3, &#x27;e&#x27;, 100, 40,  10, &#x27;h&#x27;, &#x27;a&#x27;, 50, 20, &#x27;x&#x27;, &#x27;b&#x27;, 30&#125;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>pop() 随机删除并返回集合中的一个元素</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">s = &#123;<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span>&#125;</span><br><span class="line">result = s.pop() </span><br><span class="line">print(result, s)    <span class="comment"># 1 &#123;2, 3, &#x27;b&#x27;, &#x27;a&#x27;&#125;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>remove（）删除集合中的指定元素</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">s = &#123;<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span>&#125;</span><br><span class="line">s.remove(<span class="number">1</span>)</span><br><span class="line">print(s)  <span class="comment"># &#123;2, 3, &#x27;b&#x27;, &#x27;a&#x27;&#125;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>clear（） 清空集合</p>
</li>
<li><p>copy（） 对集合浅拷贝</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">s = &#123;<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span>&#125;</span><br><span class="line"></span><br><span class="line">s2 = s.copy()</span><br><span class="line">print(s2)  <span class="comment"># &#123;1, 2, 3, &#x27;b&#x27;, &#x27;a&#x27;&#125;</span></span><br><span class="line">print(id(s), id(s2))    <span class="comment"># 1837354855328 1837354855104</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>类似于列表推导式，集合也支持推导式形式</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">a = &#123;x <span class="keyword">for</span> x <span class="keyword">in</span> <span class="string">&#x27;abracadabra&#x27;</span> <span class="keyword">if</span> x <span class="keyword">not</span> <span class="keyword">in</span> <span class="string">&#x27;abc&#x27;</span>&#125;</span><br><span class="line">print(a)  <span class="comment"># &#123;&#x27;d&#x27;, &#x27;r&#x27;&#125;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>集合的运算</p>
<ul>
<li><p>在对集合做运算时，不会影响原来的集合，而是返回一个运算结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">s1 = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;</span><br><span class="line">s2 = &#123;<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># &amp; 交集运算</span></span><br><span class="line">result = s1 &amp; s2  <span class="comment"># &#123;3, 4, 5&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># | 并集运算</span></span><br><span class="line">result = s1 | s2  <span class="comment"># &#123;1,2,3,4,5,6,7&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># - 差集</span></span><br><span class="line">result = s1 - s2  <span class="comment"># &#123;1, 2&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># ^ 异或集 获取只在一个集合中出现的元素</span></span><br><span class="line">result = s1 ^ s2  <span class="comment"># &#123;1, 2, 6, 7&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># &lt;= 检查一个集合是否是另一个集合的子集</span></span><br><span class="line">a = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;</span><br><span class="line">b = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;</span><br><span class="line"></span><br><span class="line">result = a &lt;= b  <span class="comment"># True</span></span><br><span class="line">result = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125; &lt;= &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;  <span class="comment"># True</span></span><br><span class="line">result = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125; &lt;= &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;  <span class="comment"># False</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># &lt; 检查一个集合是否是另一个集合的真子集</span></span><br><span class="line">result = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125; &lt; &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;  <span class="comment"># False</span></span><br><span class="line">result = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125; &lt; &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;  <span class="comment"># True</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># &gt;= 检查一个集合是否是另一个的超集</span></span><br><span class="line"><span class="comment"># &gt; 检查一个集合是否是另一个的真超集</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
</li>
</ol>
<h3 id="字典"><a href="#字典" class="headerlink" title="字典"></a>字典</h3><ol>
<li><p>另一个非常有用的python内置数据类型是字典 (dict)，字典在其他语言中可能被叫做 联合内存 或 联合数组</p>
</li>
<li><p>属于一种新的数据结构，称为映射，也称为键值对结构，每个字典中可以有多个键值对，每一个键值对称为一项（item）</p>
</li>
<li><p>作用和列表类型，都是用来存储对象的容器</p>
</li>
<li><p>列表存储数据的性能很好，但是查询数据的性能很差，字典中每一个元素都有唯一的key, 通过key可快速查找value，查询效率高</p>
</li>
<li><p>字典的key可以是任意不可变对象，int，str,bool,tuple,….，多是字符串或数字，字典的值可以是任意对象</p>
</li>
<li><p>如果一个元组只包含字符串、数字或元组，那么这个元组也可以用作关键字，但如果这个元组直接或间接的包含了可变对象，那么它就不能用作关键字</p>
</li>
<li><p>列表不能用作关键字，因为列表可以通过索引、切片或 append() 和 extend() 之类的方法来改变</p>
</li>
<li><p>使用{} 创建字典，语法：{key:value,key:value,key:value,…..}</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">d = &#123;&#125;  <span class="comment"># 创建了一个空字典</span></span><br><span class="line">print(d, type(d))  <span class="comment"># &#123;&#125; &lt;class &#x27;dict&#x27;&gt;</span></span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line">tel = &#123;<span class="string">&#x27;jack&#x27;</span>: <span class="number">4098</span>, <span class="string">&#x27;sape&#x27;</span>: <span class="number">4139</span>&#125;</span><br><span class="line">tel[<span class="string">&#x27;guido&#x27;</span>] = <span class="number">4137</span></span><br><span class="line">print(tel)  <span class="comment"># &#123;&#x27;jack&#x27;: 4098, &#x27;sape&#x27;: 4139, &#x27;guido&#x27;: 4137&#125;</span></span><br><span class="line">print(tel[<span class="string">&#x27;jack&#x27;</span>])  <span class="comment"># 4098</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>也可以使用dict() 函数创建，dict() 构造函数可以直接从键值对序列里创建字典</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">d = dict(name=<span class="string">&#x27;张三&#x27;</span>, age=<span class="number">18</span>, gender=<span class="string">&#x27;男&#x27;</span>)</span><br><span class="line">print(d)</span><br><span class="line"><span class="comment"># &#123;&#x27;name&#x27;: &#x27;张三&#x27;, &#x27;age&#x27;: 18, &#x27;gender&#x27;: &#x27;男&#x27;&#125;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>还可以将一个包含有双值子序列的序列转换为字典，双值序列，序列中只有两个值，如 [1,2]   (‘a’,3)   ‘ab’ 等</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">d1 = dict([(<span class="string">&#x27;sape&#x27;</span>, <span class="number">4139</span>), (<span class="string">&#x27;guido&#x27;</span>, <span class="number">4127</span>), (<span class="string">&#x27;jack&#x27;</span>, <span class="number">4098</span>)])</span><br><span class="line">print(d1)  <span class="comment"># &#123;&#x27;sape&#x27;: 4139, &#x27;guido&#x27;: 4127, &#x27;jack&#x27;: 4098&#125;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>字典的键不能重复，若出现重复，后面的替换前面的</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">d = &#123;</span><br><span class="line">    <span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;张三&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>,</span><br><span class="line">    <span class="string">&#x27;gender&#x27;</span>: <span class="string">&#x27;男&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;李四&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line">print(d)</span><br><span class="line"><span class="comment"># &#123;&#x27;name&#x27;: &#x27;李四&#x27;, &#x27;age&#x27;: 18, &#x27;gender&#x27;: &#x27;男&#x27;&#125;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>如果使用了字典中不存在的键，会报错</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">d = &#123;</span><br><span class="line">    <span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;张三&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>,</span><br><span class="line">    <span class="string">&#x27;gender&#x27;</span>: <span class="string">&#x27;男&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;李四&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line">print(d[<span class="string">&#x27;address&#x27;</span>])  <span class="comment"># KeyError: &#x27;address&#x27;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>可以用 del 来删除一个键值对</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">tel = &#123;<span class="string">&#x27;jack&#x27;</span>: <span class="number">4098</span>, <span class="string">&#x27;sape&#x27;</span>: <span class="number">4139</span>&#125;</span><br><span class="line"><span class="keyword">del</span> tel[<span class="string">&#x27;sape&#x27;</span>]</span><br><span class="line">tel[<span class="string">&#x27;irv&#x27;</span>] = <span class="number">4127</span></span><br><span class="line">print(tel)  <span class="comment"># &#123;&#x27;jack&#x27;: 4098, &#x27;guido&#x27;: 4137, &#x27;irv&#x27;: 4127&#125;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>len()，获取字典中键值对的个数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">d = dict(name=<span class="string">&#x27;张三&#x27;</span>, age=<span class="number">18</span>, gender=<span class="string">&#x27;男&#x27;</span>)</span><br><span class="line">print(len(d))  <span class="comment"># 3</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>in和not in，检查字典是否包含指定的键</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">tel = &#123;<span class="string">&#x27;jack&#x27;</span>: <span class="number">4098</span>, <span class="string">&#x27;sape&#x27;</span>: <span class="number">4139</span>&#125;tel = “</span><br><span class="line">b1 = <span class="string">&#x27;guido&#x27;</span> <span class="keyword">in</span> tel</span><br><span class="line">print(b1)  <span class="comment"># True</span></span><br><span class="line">   </span><br><span class="line">b2 = <span class="string">&#x27;jack&#x27;</span> <span class="keyword">not</span> <span class="keyword">in</span> tel</span><br><span class="line">print(b2)  <span class="comment"># False</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>get(key[, default]) ，根据key获取字典中的value，如果获取的key不存在，会返回None，也可以指定一个默认值作为第二个参数，获取不到值时将会返回默认值，更安全，不会异常</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">d = &#123;</span><br><span class="line">    <span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;张三&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>,</span><br><span class="line">    <span class="string">&#x27;gender&#x27;</span>: <span class="string">&#x27;男&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">print(d.get(<span class="string">&#x27;name&#x27;</span>))  <span class="comment"># 张三</span></span><br><span class="line">print(d.get(<span class="string">&#x27;address&#x27;</span>, <span class="string">&#x27;广东省&#x27;</span>))  <span class="comment"># 广东省</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>对一个字典执行 list(d) 将返回包含该字典中所有键的列表，按插入次序排列，如需其他排序，要使用 sorted(d)</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">new_tel1 = list(tel)</span><br><span class="line">print(new_tel1)  <span class="comment"># [&#x27;jack&#x27;, &#x27;guido&#x27;, &#x27;irv&#x27;]</span></span><br><span class="line"></span><br><span class="line">new_tel2 = sorted(tel)</span><br><span class="line">print(new_tel2)  <span class="comment"># [&#x27;guido&#x27;, &#x27;irv&#x27;, &#x27;jack&#x27;]</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>字典推导是可以从任意的键值表达式中创建字典</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">d2 = &#123;x: x ** <span class="number">2</span> <span class="keyword">for</span> x <span class="keyword">in</span> (<span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>)&#125;</span><br><span class="line">print(d2)  <span class="comment"># &#123;2: 4, 4: 16, 6: 36&#125;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>当关键字是简单字符串时，有时直接通过关键字参数来指定键值对更方便</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">d3 = dict(sape=<span class="number">4139</span>, guido=<span class="number">4127</span>, jack=<span class="number">4098</span>)</span><br><span class="line">print(d3)  <span class="comment"># &#123;&#x27;sape&#x27;: 4139, &#x27;guido&#x27;: 4127, &#x27;jack&#x27;: 4098&#125;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>修改字典的操作</p>
<ul>
<li><p>通过d[key] = value 修改， 如果key存在则覆盖，不存在则添加</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">d = &#123;</span><br><span class="line">    <span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;张三&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>,</span><br><span class="line">    <span class="string">&#x27;gender&#x27;</span>: <span class="string">&#x27;男&#x27;</span>,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">d[<span class="string">&#x27;name&#x27;</span>] = <span class="string">&#x27;李华&#x27;</span>  <span class="comment"># 修改字典的key-value</span></span><br><span class="line">d[<span class="string">&#x27;address&#x27;</span>] = <span class="string">&#x27;上海&#x27;</span>  <span class="comment"># 向字典中添加key-value</span></span><br><span class="line"></span><br><span class="line">print(d)  <span class="comment"># &#123;&#x27;name&#x27;: &#x27;李华&#x27;, &#x27;age&#x27;: 18, &#x27;gender&#x27;: &#x27;男&#x27;, &#x27;address&#x27;: &#x27;上海&#x27;&#125;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>setdefault(key[, default]) ，可以用来向字典中添加key-value，如果key已经存在于字典中，则返回key的值，不会对字典做任何操作，如果key不存在，则向字典中添加这个key，并设置value</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">d = &#123;</span><br><span class="line">    <span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;张三&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>,</span><br><span class="line">    <span class="string">&#x27;gender&#x27;</span>: <span class="string">&#x27;男&#x27;</span>,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">result = d.setdefault(<span class="string">&#x27;name&#x27;</span>, <span class="string">&#x27;李华&#x27;</span>)</span><br><span class="line">print(result, d)  <span class="comment"># 张三 &#123;&#x27;name&#x27;: &#x27;张三&#x27;, &#x27;age&#x27;: 18, &#x27;gender&#x27;: &#x27;男&#x27;&#125;</span></span><br><span class="line"></span><br><span class="line">result = d.setdefault(<span class="string">&#x27;address&#x27;</span>, <span class="string">&#x27;上海&#x27;</span>)</span><br><span class="line">print(result, d)  <span class="comment"># 上海 &#123;&#x27;name&#x27;: &#x27;张三&#x27;, &#x27;age&#x27;: 18, &#x27;gender&#x27;: &#x27;男&#x27;, &#x27;address&#x27;: &#x27;上海&#x27;&#125;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>update([other])，将其他字典中的 key-value 添加到当前字典中，如果有重复的key，后面的会覆盖当前的。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">d1 = &#123;<span class="string">&#x27;a&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;b&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;c&#x27;</span>: <span class="number">3</span>&#125;</span><br><span class="line">d2 = &#123;<span class="string">&#x27;d&#x27;</span>: <span class="number">4</span>, <span class="string">&#x27;e&#x27;</span>: <span class="number">5</span>, <span class="string">&#x27;f&#x27;</span>: <span class="number">6</span>, <span class="string">&#x27;a&#x27;</span>: <span class="number">7</span>&#125;</span><br><span class="line">d1.update(d2)</span><br><span class="line"></span><br><span class="line">print(d1)  <span class="comment"># &#123;&#x27;a&#x27;: 7, &#x27;b&#x27;: 2, &#x27;c&#x27;: 3, &#x27;d&#x27;: 4, &#x27;e&#x27;: 5, &#x27;f&#x27;: 6&#125;</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>删除字典的操作</p>
<ul>
<li><p>可以使用 del 来删除字典中的 key-value</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">d= &#123;<span class="string">&#x27;a&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;b&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;c&#x27;</span>: <span class="number">3</span>&#125;</span><br><span class="line"><span class="keyword">del</span> d[<span class="string">&#x27;a&#x27;</span>]</span><br><span class="line">print(d) <span class="comment">#&#123;&#x27;b&#x27;: 2, &#x27;c&#x27;: 3&#125;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>popitem()，随机删除字典中的一个键值对，一般都会删除最后一个键值对，会将删除的key-value作为返回值返回，当使用popitem()删除一个空字典时，会抛出异常 KeyError: ‘popitem(): dictionary is empty’</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">d = &#123;<span class="string">&#x27;a&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;b&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;c&#x27;</span>: <span class="number">3</span>&#125;</span><br><span class="line">result = d.popitem()</span><br><span class="line">print(result, d)  <span class="comment"># (&#x27;c&#x27;, 3) &#123;&#x27;a&#x27;: 1, &#x27;b&#x27;: 2&#125;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>pop(key[, default])，根据key删除字典中的key-value，会将被删除的value返回，如果删除不存在的key，会抛出异常，如果指定了默认值，再删除不存在的key时，不会报错，而是直接返回默认值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">d = &#123;<span class="string">&#x27;a&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;b&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;c&#x27;</span>: <span class="number">3</span>&#125;</span><br><span class="line"></span><br><span class="line">result = d.pop(<span class="string">&#x27;a&#x27;</span>)</span><br><span class="line">print(d, result)  <span class="comment"># &#123;&#x27;b&#x27;: 2, &#x27;c&#x27;: 3&#125;  1</span></span><br><span class="line">result = d.pop(<span class="string">&#x27;z&#x27;</span>, <span class="number">100</span>)</span><br><span class="line">print(d, result)  <span class="comment"># &#123;&#x27;b&#x27;: 2, &#x27;c&#x27;: 3&#125; 100</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>clear()用来清空字典</p>
</li>
<li><p>copy()，用于对字典进行浅拷贝，浅拷贝会简单复制对象内部的值，如果值也是一个可变对象，这个可变对象不会被复制</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">d = &#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;张三&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>&#125;</span><br><span class="line">d1 = &#123;<span class="string">&#x27;a&#x27;</span>: d, <span class="string">&#x27;b&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;c&#x27;</span>: <span class="number">3</span>&#125;</span><br><span class="line">d1[<span class="string">&#x27;a&#x27;</span>][<span class="string">&#x27;name&#x27;</span>] = <span class="string">&quot;李华&quot;</span></span><br><span class="line">d2 = d1.copy()</span><br><span class="line"></span><br><span class="line">print(d1, id(d1))  <span class="comment"># &#123;&#x27;a&#x27;: &#123;&#x27;name&#x27;: &#x27;李华&#x27;, &#x27;age&#x27;: 18&#125;, &#x27;b&#x27;: 2, &#x27;c&#x27;: 3&#125; 1831165784768</span></span><br><span class="line">print(d2, id(d2))  <span class="comment"># &#123;&#x27;a&#x27;: &#123;&#x27;name&#x27;: &#x27;李华&#x27;, &#x27;age&#x27;: 18&#125;, &#x27;b&#x27;: 2, &#x27;c&#x27;: 3&#125; 1831165692672</span></span><br></pre></td></tr></table></figure>
</li>
<li></li>
</ul>
</li>
</ol>
<ol start="20">
<li><p>遍历字典</p>
<ul>
<li><p>keys() , 该方法会返回一个序列，序列中保存有字典的所有的key</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">d = &#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;张三&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;gender&#x27;</span>: <span class="string">&#x27;男&#x27;</span>&#125;</span><br><span class="line"><span class="keyword">for</span> k <span class="keyword">in</span> d.keys():</span><br><span class="line">    print(k, d[k])</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">name 张三</span></span><br><span class="line"><span class="string">age 18</span></span><br><span class="line"><span class="string">gender 男</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>values()，该方法会返回一个序列，序列中保存有字典所有的值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">d = &#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;张三&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;gender&#x27;</span>: <span class="string">&#x27;男&#x27;</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> v <span class="keyword">in</span> d.values():</span><br><span class="line">    print(v)</span><br><span class="line">    </span><br><span class="line"></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">张三  18  男</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>items()，该方法会返回一个序列，序列中包含双值子序列，双值分别是，字典中的 key 和 value</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">d = &#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;张三&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;gender&#x27;</span>: <span class="string">&#x27;男&#x27;</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> k, v <span class="keyword">in</span> d.items():</span><br><span class="line">    print(k, <span class="string">&#x27;=&#x27;</span>, v)</span><br><span class="line"> </span><br><span class="line"></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">name = 张三</span></span><br><span class="line"><span class="string">age = 18</span></span><br><span class="line"><span class="string">gender = 男</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
</li>
</ol>
<h3 id="循环的技巧"><a href="#循环的技巧" class="headerlink" title="循环的技巧"></a>循环的技巧</h3><ol>
<li><p>在字典中循环时，用 item() 方法可将关键字和对应的值同时取出</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">stuInfo = &#123;<span class="string">&#x27;姓名&#x27;</span>: <span class="string">&#x27;张三&#x27;</span>, <span class="string">&#x27;年龄&#x27;</span>: <span class="number">15</span>, <span class="string">&#x27;性别&#x27;</span>: <span class="string">&#x27;男&#x27;</span>&#125;</span><br><span class="line"><span class="keyword">for</span> k, v <span class="keyword">in</span> stuInfo.items():</span><br><span class="line">    print(k, v)</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">姓名 张三</span></span><br><span class="line"><span class="string">年龄 15</span></span><br><span class="line"><span class="string">性别 男</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>在序列中循环时，用 enumerate() 函数可以将索引位置和其对应的值同时取出</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> k, v <span class="keyword">in</span> enumerate([<span class="string">&#x27;姓名&#x27;</span>, <span class="string">&#x27;年龄&#x27;</span>, <span class="string">&#x27;性别&#x27;</span>]):</span><br><span class="line">    print(k, v)</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">0 姓名</span></span><br><span class="line"><span class="string">1 年龄</span></span><br><span class="line"><span class="string">2 性别</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>同时在两个或多个序列中循环时，用zip() 函数将其内元素一一匹配</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">questions = [<span class="string">&#x27;姓名&#x27;</span>, <span class="string">&#x27;年龄&#x27;</span>, <span class="string">&#x27;性别&#x27;</span>]</span><br><span class="line">answers = [<span class="string">&#x27;张三&#x27;</span>, <span class="number">18</span>, <span class="string">&#x27;男&#x27;</span>]</span><br><span class="line"><span class="keyword">for</span> k, v <span class="keyword">in</span> zip(questions, answers):</span><br><span class="line">    print(&#123;<span class="string">&#x27;&#123;0&#125;:&#123;1&#125;&#x27;</span>.format(k, v)&#125;)</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">&#123;&#x27;姓名:张三&#x27;&#125;</span></span><br><span class="line"><span class="string">&#123;&#x27;年龄:18&#x27;&#125;</span></span><br><span class="line"><span class="string">&#123;&#x27;性别:男&#x27;&#125;</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>逆向循环一个序列，可以先正向定位㤡，然后调用 reversed() 函数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> reversed(range(<span class="number">1</span>, <span class="number">10</span>, <span class="number">2</span>)):</span><br><span class="line">    print(i)</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">9</span></span><br><span class="line"><span class="string">7</span></span><br><span class="line"><span class="string">5</span></span><br><span class="line"><span class="string">3</span></span><br><span class="line"><span class="string">1</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>按某个指定顺序循环一个序列，可以用 sorted() 函数，它可以在不改动原序列的基础上返回一个新的拍好序的序列</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">basket = [<span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;orange&#x27;</span>, <span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;pear&#x27;</span>, <span class="string">&#x27;orange&#x27;</span>, <span class="string">&#x27;banana&#x27;</span>]</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> sorted(set(basket)):</span><br><span class="line">    print(i)</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">apple</span></span><br><span class="line"><span class="string">banana</span></span><br><span class="line"><span class="string">orange</span></span><br><span class="line"><span class="string">pear</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>在循环时修改列表内容时，可以改为创建一个新列表，简单安全</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> math</span><br><span class="line"></span><br><span class="line">raw_data = [<span class="number">56.2</span>, float(<span class="string">&#x27;NaN&#x27;</span>), <span class="number">51.7</span>, <span class="number">55.3</span>, <span class="number">52.5</span>, float(<span class="string">&#x27;NaN&#x27;</span>), <span class="number">47.8</span>]</span><br><span class="line">filter_data = []</span><br><span class="line"><span class="keyword">for</span> item <span class="keyword">in</span> raw_data:</span><br><span class="line">    <span class="keyword">if</span> <span class="keyword">not</span> math.isnan(item):</span><br><span class="line">        filter_data.append(item)</span><br><span class="line">print(filter_data)</span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">[56.2, 51.7, 55.3, 52.5, 47.8]</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>

</li>
</ol>
<h3 id="深入条件控制"><a href="#深入条件控制" class="headerlink" title="深入条件控制"></a>深入条件控制</h3><ol>
<li><p>while 和 if 条件句中可以使用任意操作，不仅仅是比较操作</p>
</li>
<li><p>比较操作符 in 和 not in 检验一个值是否在或不在一个序列中。</p>
</li>
<li><p>操作符 is 和 is not 比较两个对象是不是同一个对象，这只对像列表这样的可变对象比较重要。</p>
</li>
<li><p>所有的比较操作符都有相同的优先级，且优先级比数字运算符低</p>
</li>
<li><p>比较操作可以传递，例如 a&lt;b ==c ，会校验 a小于b 并且 b等于c</p>
</li>
<li><p>比较操作可以通过布尔运算符 and 和 or 来组合</p>
</li>
<li><p>比较操作或任何布尔运算的结果都可以用 not 来取反</p>
</li>
<li><p>这些操作符的优先级低于比较操作符，其中，not优先级最高，or 优先级最低，因此 A and not B or C 等价于 (A and (not B)) or C</p>
</li>
<li><p>布尔运算符 and 和 or 也被称为短路运算符，它们的参数从左至于右解析，一旦可以确定结果解析就停止。当用作普通值而非布尔值时，短路操作符的返回值通常是最后一个变量</p>
</li>
<li><p>也可以把比较操作或者逻辑表达式的结果赋值给一个变量</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">str1，str2，str3=<span class="string">&#x27;&#x27;</span>,<span class="string">&#x27;Tron&#x27;</span>,<span class="string">&#x27;Ham&#x27;</span></span><br><span class="line">non_null = str1 <span class="keyword">or</span> str2 <span class="keyword">or</span> str3</span><br><span class="line"></span><br><span class="line"><span class="comment"># Tron</span></span><br></pre></td></tr></table></figure>

</li>
</ol>
<h3 id="比较序列和其他类型"><a href="#比较序列和其他类型" class="headerlink" title="比较序列和其他类型"></a>比较序列和其他类型</h3><ol>
<li><p>序列对象通常可以与相同序列类型的其他对象比较，这种比较使用 字典式 顺序</p>
</li>
<li><p>首先比较开头的两个对应元素，如果不等则比较结果由此确定</p>
</li>
<li><p>如果相等则比较之后的两个元素，以此类推，直到有一个序列被耗尽</p>
</li>
<li><p>如果要比较的两元素本身又是相同类型的序列，则会递归执行字典式顺序比较</p>
</li>
<li><p>如果两序列中所有的对应元素都相等，则两序列也将被视为相等</p>
</li>
<li><p>如果一个序列是另一个序列的初始子序列，则较短的序列被视为较小</p>
</li>
<li><p>对于字符串来说，字典式顺序是使用 Unicode 码位序号 对单个字符排序</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>) &lt; (<span class="number">1</span>,<span class="number">2</span>,<span class="number">4</span>)</span><br><span class="line">[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>] &lt; [<span class="number">1</span>,<span class="number">2</span>,<span class="number">4</span>]</span><br><span class="line"><span class="string">&#x27;ABC&#x27;</span> &lt; <span class="string">&#x27;C&#x27;</span> &lt; <span class="string">&#x27;Pascal&#x27;</span> &lt; <span class="string">&#x27;Python&#x27;</span></span><br><span class="line">(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>) &lt; (<span class="number">1</span>,<span class="number">2</span>,<span class="number">4</span>) </span><br><span class="line">(<span class="number">1</span>,<span class="number">2</span>) &lt; (<span class="number">1</span>,<span class="number">2</span>,<span class="number">-1</span>)</span><br><span class="line">(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>) &lt; (<span class="number">1.0</span>,<span class="number">2.0</span>,<span class="number">3.0</span>)</span><br><span class="line">(<span class="number">1</span>,<span class="number">2</span>,(<span class="string">&#x27;aa&#x27;</span>,<span class="string">&#x27;ab&#x27;</span>)) &lt; (<span class="number">1</span>,<span class="number">2</span>,(<span class="string">&#x27;abc&#x27;</span>,<span class="string">&#x27;a&#x27;</span>),<span class="number">4</span>)</span><br></pre></td></tr></table></figure>

</li>
</ol>

    </div>

    
    
    

    
      <div>
       <div>
  
    <div style="text-align:center;color:#bfbfbf;font-size:16px;">
      <span>-------- 本文结束 </span>
      <i class="fa fa-snowflake-o"></i>
      <span> 感谢阅读 --------</span>
    </div>
  
</div>
      </div>
    

    <footer class="post-footer">
          <div class="reward-container">
  <div></div>
  <button onclick="document.querySelector('.post-reward').classList.toggle('active');">
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wechatpay.png" alt="shanlu 微信">
        <span>微信</span>
      </div>

  </div>
</div>

          <div class="post-tags">
              <a href="/tags/python/" rel="tag"><i class="fa fa-tag"></i> python</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2020/09/26/python/%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/" rel="prev" title="流程控制语句">
                  <i class="fa fa-chevron-left"></i> 流程控制语句
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2020/09/27/python/%E6%A8%A1%E5%9D%97/" rel="next" title="模块">
                  模块 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
  
  
  



      
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

    </div>
  </main>

  <footer class="footer">
    <div class="footer-inner">
      

      

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">shanlu</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">225k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">3:25</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>


    <script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

    </div>
  </footer>

  
  <script size="300" alpha="0.6" zIndex="-1" src="//cdn.jsdelivr.net/npm/ribbon.js@1.0.2/dist/ribbon.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.0/lib/anime.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>




  <script src="/js/local-search.js"></script>















  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>








  

  
<script>
NexT.utils.loadComments('#valine-comments', () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js', () => {
    new Valine(Object.assign({
      el  : '#valine-comments',
      path: "/2020/09/27/python/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/",
    }, {"enable":true,"appId":"M0TTE46ti90I3BUUJqD5WYyD-gzGzoHsz","appKey":"UelegRsJXosPYDakAbUdO5e8","notify":true,"verify":false,"placeholder":"留下评论吧","avatar":"mm","meta":["nick","mail","link"],"pageSize":10,"lang":"zh-cn","visitor":true,"comment_count":true,"recordIP":false,"serverURLs":null,"enableQQ":false,"requiredFields":[]}
    ));
  }, window.Valine);
});
</script>

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"position":"left","width":225,"height":450},"mobile":{"show":true},"react":{"opacity":0.7},"log":false});</script></body>
</html>
